package com.xiaoyu.cloudmusic.controller;

import com.xiaoyu.cloudmusic.pojo.PlayListDetail;
import com.xiaoyu.cloudmusic.pojo.Song;
import com.xiaoyu.cloudmusic.pojo.Tracks;
import com.xiaoyu.cloudmusic.service.MusicServiceImpl;
import com.xiaoyu.cloudmusic.service.PlayListServiceImpl;
import com.xiaoyu.cloudmusic.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@ResponseBody
public class MusicController {
    @Autowired
    MusicServiceImpl musicService;

    @Autowired
    PlayListServiceImpl playListService;

    /**
     *
     * @param id 是所有公共歌单的id，包括自有的和用户创建的
     * @return Result 返回结果类
     */
    @RequestMapping("/playList/SongList")
    public Result getSongList(@RequestParam int id){
        try {
//            List<Song> songAllList = musicService.getAllSong();//获取到所有的歌单数据
//            List<Song> songList = new ArrayList<>();
////            随机返回20首歌
//            Set<Integer> remove = new HashSet<>();
//            while (remove.size() < 20){
//                int i = (int)(Math.random()*30);
//                remove.add(i);
//            }
//            for (Integer integer : remove) {
//                songList.add(songAllList.get(integer));
//            }
            /*
             * 根据用户的公开歌单返回到发现歌曲的列表当中，可以进行共享！！！
             * */
            System.out.println(id);
            if(id >= 99990){
                List<Song> songList=musicService.getRandSong(20);
                return Result.ok().put("songList",songList);
            }else {
                PlayListDetail playListDetail = playListService.getPlayListDetail(id);
                return Result.ok().put("data",playListDetail);
            }

        }catch (Exception e){
            System.out.println(e.getCause());
            return Result.error(404,"查询错误!");
        }
    }



}
